import pymssql


def update(host, user, pwd, db):
    """服务器，账号，密码，数据库名"""
    connect = pymssql.connect(host, user, pwd, db)
    if connect:
        print('连接成功')
        sql = "update POSIdentity set bLocked = '0' WHERE szPosItemID ='10000002'"
        cursor = connect.cursor() #创建游标
        cursor.execute(sql) #执行sql
        connect.commit()  #提交数据
        cursor.close()  #关闭游标
        connect.close() #关闭连接
    else:
        return connect


def select(host, user, pwd, db):
    """服务器，账号，密码，数据库名"""
    connect = pymssql.connect(host, user, pwd, db)
    if connect:
        print('连接成功')
        sql = "SELECT * from POSIdentity WHERE szPosItemID ='10000002'"
        cursor = connect.cursor() #创建游标
        cursor.execute(sql) #执行sql
        row = cursor.fetchone() #读取查询结果
        while row:
            print(f"ID=%s" % row[0])
            row = cursor.fetchone()
        cursor.close()  #关闭游标
        connect.close() #关闭连接
    else:
        return connect


if __name__ == '__main__':
    update("10.3.16.131", 'sa', 'Wincor2016', 'TPCentralDB')
    select("10.3.16.131", 'sa', 'Wincor2016', 'TPCentralDB')